﻿package bdz.dms.core {
	import bdz.common.model.XMLLoader;
	import bdz.dms.data.DMSCollection;
	import bdz.dms.events.DMSEvent;
	import bdz.dms.vo.ContentVO;
	import bdz.dms.vo.SectionVO;
	import bdz.dms.vo.TemplateVO;
	import bdz.dms.vo.UserVO;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	/**
	* ...
	* @author Default
	*/
	internal class DMSDataHook extends XMLLoader {
		public var processResults:Boolean = false;
		function DMSDataHook(_event:String) {
			super(_event);
		}
		private function convertResults(str:ByteArray):void {
			var xm:XML = new XML(str);
			var data:*;
			if (getDataType(xm) == null) {
				dispatchEvent(new DMSEvent(DMSEvent.PROCESSING_ERROR, null));
				return;
			}
			switch(getDataType(xm)) {
				case "section":
					data = sectionParse(xm);
				break;
				case "content":
					data = contentParse(xm);
				break;
				case "user":
					data = usersParse(xm);
				break;
				case "templates":
					data = templateParse(xm);
				break;
				case "section_and_content":
					data = sectionContentParse(xm);
				break;
				case "applied_templates":
					data = appliedTemplatesParse(xm);
				break;
				case "error":
					trace(xm);
					trace("dms error?");
				break;
				default:
					trace("?default?");
				break;
				
			}
			
			if(data){
				if(data.length){
					dispatchEvent(new DMSEvent(eventType, data.length == 1 ? data[0] : data));
				}else{
					dispatchEvent(new DMSEvent(eventType, null));
				}
			}else{
				dispatchEvent(new DMSEvent(DMSEvent.PROCESSING_ERROR, null));
			}
		}
		private function getDataType(xm:XML):String {
			if (xm.hasOwnProperty("dms_sections")) {
				return "section";
			}
			if (xm.hasOwnProperty("dms_contents")) {
				return "content";
			}
			if (xm.hasOwnProperty("dms_user")) {
				return "user";
			}
			if (xm.hasOwnProperty("dms_templates")) {
				return "templates";
			}
			if (xm.hasOwnProperty("dms_sectionandcontent")){
				return "section_and_content";
			}
			if(xm.hasOwnProperty("dms_appliedtemplates")){
				return "applied_templates";
			}
			if (xm.hasOwnProperty("dms_error")) {
				return "error";
			}
			
			return null;
		}
		private function appliedTemplatesParse(xm:XML):Array{
			var arr:Array = new Array();
			for each(var info:XML in xm.dms_appliedtemplates){
				arr.push({tsection_id:info.tsection_id, template_id:info.template_id, section_id:info.section_id});
			}
			return arr;
		}
		private function sectionContentParse(xm:XML):DMSCollection{
			
			var ret:DMSCollection = new DMSCollection();
			for each(var info:XML in xm.dms_sectionandcontent){
				if(!info.hasOwnProperty("content_id")){
					if(info.hasOwnProperty("section_id")){
						ret.push(new SectionVO(info));
					}
				}else{
					if(info.hasOwnProperty("content_id")){
						ret.push(new ContentVO(info));
					}
				}
			}
			return ret;
		}
		private function usersParse(xm:XML):DMSCollection{
			var ret:DMSCollection = new DMSCollection();
			for each(var info:XML in xm.dms_user) {
				if(info.hasOwnProperty("user_id")){
					ret.push(new UserVO(info));
				}
			}
			return ret;
		}
		private function templateParse(xm:XML):DMSCollection{
			var ret:DMSCollection = new DMSCollection();
			for each(var info:XML in xm.dms_templates) {
				if(info.hasOwnProperty("template_id")){
					ret.push(new TemplateVO(info));
				}
			}
			return ret;
		}
		private function contentParse(xm:XML):DMSCollection{
			var ret:DMSCollection = new DMSCollection();
			for each(var info:XML in xm.dms_contents) {
				if(info.hasOwnProperty("content_id")){
					ret.push(new ContentVO(info));
				}
			}
			return ret;
		}
		private function sectionParse(xm:XML):DMSCollection {
			var ret:DMSCollection = new DMSCollection();
			for each(var info:XML in xm.dms_sections) {
				if(info.hasOwnProperty("section_id")){
					ret.push(new SectionVO(info));
				}
			}
			return ret;
		}
		override protected function resultHandler(event:Event):void {
			var outdata:ByteArray = httpconnection.data;
			try{
				outdata.uncompress();
			}catch(e:Error){
				trace("hook error");
				trace(e.message + "\n" + outdata);
			}
			
			if (processResults) {
				convertResults(outdata);
			}else{
				dispatchEvent(new DMSEvent(eventType, outdata));
			}
		}
	}
	
}